import Vue from 'vue'
import VueRouter from 'vue-router'
import main from '../components/main'
import parentView from '../components/parent-view'
import store from 'store'
import config from 'public/config/config.js'
import {message} from 'ant-design-vue'

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    name:'blog',
    component: ()=>import('../views/blog/index.vue'),
  },
  {
    path: '/admin',
    name: 'admin',
    component: main,
    meta:{title:'VKadmin'},
    children:[
      {
        path:'/',
        name:'home',
        component:()=>import('../views/home.vue')
      }
    ]
  },
  {
    path:'/article',
    name:'article',
    component:main,
    children:[
      {
        path:'article_list',
        name:'article_list',
        component:()=>import('../views/article/article_list.vue')
      },
      {
        path:'article_detail',
        name:'article_detail',
        component:()=>import('../views/article/article_detail.vue')
      },
      {
        path:'article_classify',
        name:'article_classify',
        component:()=>import('../views/article/article_classify.vue')
      }
    ]
  },
  {
    path:'/product',
    name:'product',
    component:main,
    children:[
      {
        path:'product_list',
        name:'product_list',
        component:()=>import('../views/product/product_list.vue')
      },
      {
        path:'product_classify',
        name:'product_classify',
        component:()=>import('../views/product/product_classify.vue')
      },
      {
        path:'product_reply',
        name:'product_reply',
        component:()=>import('../views/product/product_reply.vue')
      },
      {
        path:'product_spec',
        name:'product_spec',
        component:()=>import('../views/product/product_spec.vue')
      },
      {
        path:'product_detail',
        name:'product_detail',
        component:()=>import('../views/product/product_detail.vue')
      }
    ]
  },
  {
    path:'/setting',
    name:'setting',
    component: main,
    children:[
      {
        path:'system_config',
        name:'system_config',
        component:()=>import('../views/setting/system_config.vue')
      },
      {
        path:'authority',
        name:'authority',
        component:parentView,
        children:[
          {
            path:'system_authority',
            name:'system_authority',
            component:()=>import('../views/setting/authority/system_authority.vue')
          },
          {
            path:'system_menus',
            name:'system_menus',
            component:()=>import('../views/setting/authority/system_menus.vue')
          },
          {
            path:'system_admin',
            name:'system_admin',
            component:()=>import('../views/setting/authority/system_admin.vue')
          },
        ]
      },
      {
        path:'system_data',
        name:'system_data',
        component:parentView,
        children:[
          {
            path:'sign',
            name:'sign',
            component:()=>import('../views/setting/system_data/sign.vue')
          },
        ]
      }
    ]
  },
  {
    path:'/admin/login',
    name:'login',
    component:()=>import ('../views/login.vue')
  },
  {
    path: '*',
    name: '404',
    component: ()=>import('../views/404.vue')
  }
]

//以下代码解决路由地址重复的报错问题
const originalPush = VueRouter.prototype.push
  VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  mode: 'history',  //history模式时,要放出下面的代码
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next)=>{
  // console.log("to",to,from)
  if(to.path != "/admin/login"){
    // store.commit("admin/checkLogin")
    store.dispatch('admin/checkAuthority',to.path).then(e=>{
      if(e.status == 200){
        next()
      }else if(e.status == 500){
        message.error(e.msg)
      }
    })
  }else{
    next()
  }
})

router.afterEach((to,from)=>{
  // console.log("afterEach",to,from)
  store.state.menu.defaultTo = to
  store.commit('menu/createTag',to)
  if(to.meta.title){
    document.title = to.meta.title
  }else{
    document.title = config.title
  }
})

export default router
